<template>
  <div class="content">
    <dt-search ref="search" :label-width="100">
      <Button type="primary" v-if="unlocking('0207050601')" @click='onExport'>导出</Button>
    </dt-search>
    
    <div class="statistics">
      <div class="item" v-for="item in options" :key="item.key">
        <Tooltip :content="item.tips" placement="top-start" :offset="-12">
          <Icon v-if="item.tips" type="ios-help-circle-outline" size="16" color="#FF3030" />
        </Tooltip>
        <p>{{ item.title }}：{{ statistics[item.key] }}元</p>
      </div>
    </div>
    
    <dt-grid ref="table" page :onFetch='onFetch' />
	</div>
</template>

<script>
  import moment from 'moment'
  import Column from './opt/Column.js'
  import SearchOpt from './opt/SearchOpt.js'
  import financialMgr from '@/biz/report/financialReport.js'
  
  export default {
    data() {
      return {
        search: {
          bonusMonth: moment(new Date()).format('YYYY-MM'),
          startMonth: null,
          endMonth: null
        },
        options: [{
          key: 'bonusMoney',
          title: '应发激励奖金'
        }],
        statistics: {}
      }
    },
    mounted() {
      this.$refs.table.init(this, new Column())
      this.$refs.search.init(this.$refs.table, new SearchOpt(this.search), this.search)
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        this.search = data
        if (data.bonusMonth || data.housesId) {
          this.getStatistics()
          return financialMgr.incentiveList(data)
        } else {
          let title = '案场名称，发放月份至少输入一个'
          this.$Message.error(title)
          return Promise.reject(new Error(title))
        }
      },
      onExport() {
        financialMgr.incentiveListExport(this.search)
      },
      getStatistics() {
        financialMgr.incentiveStatistics(this.search).then(res => {
          this.statistics = {
            bonusMoney: res ? this.$tool.formatterMoney(res) : '--'
          }
        })
      }
    },
  }
</script>

<style lang="scss" scoped>
  .content {
    padding: 20px;
  }
  
  .statistics {
    display: flex;
    .item {
      margin-right: 12px;
      padding: 1px 5px;
      border-radius: 2px;
      color: #fa795e;
      background-color: #fff5f5;
      display: flex;
      align-items: center;
      p {
        margin-left: 3px;
      }
    }
  }
</style>
